import cv2
image=cv2.imread('hat.png',cv2.IMREAD_GRAYSCALE)
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
cv2.imshow("Input",image)

eroded_img1=cv2.erode(image,kernel)
cv2.imshow("2Eroded(3*3)",eroded_img1)


dilated_img2=cv2.dilate(image,kernel,iterations=1)
cv2.imshow("3-dilate(3*3)",dilated_img2)


image1=cv2.erode(image,kernel)
image2=cv2.dilate(image1,kernel)
cv2.imshow("4Output",image2)


image1=cv2.dilate(image,kernel)
image2=cv2.erode(image1,kernel)
cv2.imshow("5put",image2)


gradient =cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kernel)
cv2.imshow("6Gradient",gradient)


tophat=cv2.morphologyEx(image,cv2.MORPH_TOPHAT,kernel)
blackhat=cv2.morphologyEx(image,cv2.MORPH_BLACKHAT,kernel)
cv2.imshow("7TopHat",tophat)
cv2.imshow("8BlackHat",blackhat)

cv2.waitKey()
cv2.destroyAllWindows()
